<template>
  <div class="admininformanage">
    <!-- 管理员信息管理 -->
    <div class="adincontain">
      <div class="addadmin">
        <p class="add" @click="dialogFormVisible = true">添加管理员</p>
        <!-- 添加管理员对话框 -->
        <el-dialog title="添加管理员" :visible.sync="dialogFormVisible">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            class="demo-ruleForm"
          >
            <el-form-item prop="name">
              <el-input
                v-model="ruleForm.name"
                placeholder="管理员名称"
              ></el-input>
            </el-form-item>
            <el-form-item prop="pass">
              <el-input v-model="ruleForm.pass" placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item prop="phone">
              <el-input
                v-model="ruleForm.phone"
                placeholder="手机号"
              ></el-input>
            </el-form-item>
            <el-form-item prop="address">
              <el-input
                v-model="ruleForm.address"
                placeholder="地址"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-radio-group v-model="ruleForm.resource">
                <el-radio label="普通管理员"></el-radio>
                <el-radio label="超级管理员"></el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogFormVisible = false"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
      <!-- 管理员表格信息 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="管理员" width="200">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.admin }}</span>
          </template>
        </el-table-column>
        <el-table-column label="电话" width="200">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.phone }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名">
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.name }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="住址" width="280">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.address }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="open">编辑</el-button>
            <el-popconfirm title="确定删除此管理员吗？">
              <el-button
                size="mini"
                type="danger"
                slot="reference"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  name: "AdminInforManage",
  data() {
    return {
      tableData: [],
      ruleForm: {
        name: "",
        pass: "",
        phone: "",
        address: "",
        resource: "",
      },
      dialogFormVisible: false,
      rules: {
        name: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        pass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        address: [{ required: true, message: "请输入地址", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
      },
    };
  },
  beforeMount() {
    // 获取数据
    this.axios.get("/admininformanage").then((res) => {
      // console.log(typeof res.data.admin[0].phone);
      this.tableData = res.data.admin;
    });
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    open() {
      this.$prompt("请输入手机号", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^1[3-9]\d{9}$/,
        inputErrorMessage: "请输入正确手机号",
      })
        .then(({ value }) => {
          this.$message({
            type: "success",
            message: "你的手机号是: " + value,
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
  },
};
</script>

<style scoped lang="scss">
.admininformanage {
  width: 100%;
  height: 100%;
  background-color: white;
  .adincontain {
    margin: 0px 30px 30px 30px;
  }
  .addadmin {
    display: flex;
    justify-content: flex-end;
    .add {
      font-size: 14px;
      padding: 3px;
      border-radius: 5px;
      border: 1px solid #d3d5d7;
      cursor: pointer;
    }
  }
}
</style>